//
//  XZModifyNameVC.m
//  Teecloud
//
//  Created by PC on 2018/10/22.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZModifyNameVC.h"

@interface XZModifyNameVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTf;


@end

@implementation XZModifyNameVC
#pragma makr - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改姓名";
    self.view.backgroundColor = kWhiteColor;
    if (self.name.length > 0) {
        self.nameTf.text = self.name;
    }
//    [self.nameTf becomeFirstResponder];
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if ([string isEqualToString:@"\n"]){
        return YES;
    }
    NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.nameTf == textField){
        if ([aString length] > 8){
            return NO;
        }
    }
    return YES;
}

//确认修改
- (IBAction)confirmModifyNameButtonAction:(UIButton *)sender {
    
    /**判断原密码是否为空*/
    if (kIsEmptyString(self.nameTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入姓名" toView:self.view];
        return;
    }
    
    if (self.nameTf.text.length < 2 || self.nameTf.text.length > 8) {
        [MBProgressHUD showErrorOrSuc:@"姓名由2-8个汉字组成" toView:self.view];
        return;
    }
    if (![self.nameTf.text isValidChinese]) {
        [MBProgressHUD showErrorOrSuc:@"姓名由纯汉字组成" toView:self.view];
        return;
    }
    
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"fieldName":@"Name",
                          @"fieldValue":self.nameTf.text
                          };
    __weak typeof(self)weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=UpdateUserInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                [MBProgressHUD showErrorOrSuc:@"修改成功" toView:weakSelf.view];
                [weakSelf.navigationController popViewControllerAnimated:true];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
        }else{
            NSLog(@"%@",error);
        }
    }];
}



@end
